package com.versa.artflowlib;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.versa.lib.Versa;
import com.versa.lib.VersaBuilder;
import com.versa.lib.listeners.CompletionListener;
import com.versa.lib.listeners.ImageSavedListener;
import com.versa.lib.listeners.ProgressListener;

public class TestActivity extends Activity {
    private String TAG = "hill/MainActivity";

    private TextView tvResult;
    private Button tvPreload, tvStylize, tvStylize2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvResult = (TextView) findViewById(R.id.result);

        tvPreload = (Button) findViewById(R.id.preload);
        tvPreload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initialize();
            }
        });

        tvStylize = (Button) findViewById(R.id.style);
        tvStylize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                post_stylize(1);
            }
        });

        tvStylize2 = (Button) findViewById(R.id.style2);
        tvStylize2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                post_stylize(2);
            }
        });

        findViewById(R.id.destroy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if(mVersa == null)
//                    return;
//                mVersa.destroyArcade();
//                mVersa = null;
                try {
                    if (mService != null) {
                        mService.destroyVersa();
                        unbindService(mConnection);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

//    private Versa mVersa;

    private void initialize() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                VersaBuilder builder = new VersaBuilder(TestActivity.this);
//                mVersa = builder.build();
//                mVersa.initialize();
//                mVersa.setLogEnabled(true);
//                mVersa.setProgressListener(new ProgressListener() {
//                    @Override
//                    public void onUpdateProgress(final String log, boolean important) {
//                        Log.d(TAG, "onUpdateProgress");
////                        tvResult.setText(log);
//                    }
//                });
//                mVersa.setImageSavedListener(new ImageSavedListener() {
//                    @Override
//                    public void onImageSaved(String path) {
//                        Log.d(TAG, "onImageSaved");
//                    }
//                });
//                mVersa.setCompletionListsner(new CompletionListener() {
//                    @Override
//                    public void onComplete() {
//                        Log.d(TAG, "onComplete");
//                    }
//                });
//                mVersa.preload();
//            }
//        }).start();
        if(mService == null)
            bindService();
        else
            mService.init();
    }

    private void post_stylize(final int modelIndex) {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                mVersa.stylize(modelIndex);
//            }
//        }).start();
        mService.stylize();
    }

    private StylizeService mService;
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            StylizeService.StylizeBinder binder = (StylizeService.StylizeBinder) service;
            mService = binder.getService();

            mService.init();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    private void bindService() {
        Intent intent = new Intent(this, StylizeService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

}
